<?php
/*
 * @Author       :  　 @祥💥　技术支持
 * @Mail         : shejiz@qq.com
 * @Date         : 2025-07-19 09:34:15
 * @LastEditTime : 2025-07-19 09:47:39
 * @LastEditors  :  　 @祥💥　技术支持
 * @Description  : 
 * @FilePath     : \35dir\tool\qun_pic.php
 * It's up to you ^_^
 * Copyright (c) 2025 by shejiz@qq.com, All Rights Reserved. 
 */
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    header('Content-Type: application/json; charset=utf-8');
    $QqunPic = new QqunPic;
    $QqunPic->gc = '869787073';
    $QqunPic->cookie = 'RK=nClwDF27Z8; ptcz=dc9e149d6c9c74c12e778027007d2eeea2148da8e6d8d17b8e7aca0bb8605179; pac_uid=0_1XStZh0z6tJBT; _qimei_uuid42=1960b0b3730100e3e82204d0f6f51dde2f088cb11a; _qimei_fingerprint=05d48589bbcb4745bc27df4da90c864a; _qimei_q36=; _qimei_h38=b00b2421e82204d0f6f51dde0200000c41960b; omgid=0_1XStZh0z6tJBT; pgv_pvid=7497477365; _clck=3990734420|1|fxh|0; _qpsvr_localtk=0.6167678057798698; pgv_info=ssid=s6616469678; verifysession=h0173a98660700cdd9c9f1679ef330bef414e8d7b807e790ece4031f89e6a18e2ed9d3918c85ae4c0e1; fqm_pvqid=4f0366fb-5089-47d1-8f08-f40569c097a7; fqm_sessionid=f3318866-0ef7-4262-b8fd-8a77316df8d3; x-hng=lang=zh-CN&domain=qun.qq.com; uin=o0002131706; traceid=5b23732e49; tgw_l7_route=8a54d6af77b715dedda11cb104007902; skey=@bRZZaD00y; p_uin=o0002131706; pt4_token=Gb*TCCC5eUZPA4Lruro47ZTPbBcjK8KYtY0Qx2SSp3s_; p_skey=6zklesfpxiaelU1w2gh*hUPg6Ix3M9tSf-myNGATCAI_';
    $result = $QqunPic->Upload($_FILES['file']['tmp_name']);
    if (isset($result['data']['pic_url'])) {
        $url = $result['data']['pic_url'];
        $ReData = array(
            'errno' => 0,
            'data' => $url
        );
    } else {
        $ReData = array(
            'errno' => 1,
            'data' => ''
        );
    }
    echo json_encode($ReData, 256);
    exit();
}


class QqunPic
{

    private $url = 'https://qun.qq.com/cgi-bin/qunrobots/robots_image';
    public $skey = '';
    public $gc = ''; // 你自己有管理权限的群号
    public $cookie = ''; // cookie

    public function Upload($file_tmp)
    {
        // 将文件内容转换为base64编码
        $file_data = base64_encode(file_get_contents($file_tmp));
        // 构造POST请求参数
        $data = array(
            'bkn' => $this->getBkn($this->setCookieString($this->cookie)['skey']),
            'gc' => $this->gc,
            'type' => '1',
            'pic_up' => $file_data
        );
        return json_decode($this->request($this->url, $data), 1);
    }

    private function setCookieString($string)
    {
        $result = array();
        if (preg_match_all('/\s*([^;=]+)=([^;]+)/i', $string, $matches) > 0) {
            if (isset($matches[1]) && isset($matches[2])) {
                if (count($matches[1]) == count($matches[2])) {
                    foreach ($matches[1] as $handle => $key) {
                        $result[$key] = $matches[2][$handle];
                    }
                }
            }
        }
        return $result;
    }

    private function getBkn($skey)
    {
        $hash = 5381;
        for ($i = 0, $len = strlen($skey); $i < $len; ++$i) {
            $hash += ($hash << 5) + ord($skey[$i]);
        }
        return $hash & 0x7fffffff;
    }

    private function request($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
        //print_r($response);
        curl_close($ch);
        return $response;
    }
}
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>QQ群图床？</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="https://cdn.rcku.cn/ajax/libs/layui/2.7.6/css/layui.min.css" media="all">
    <link rel="stylesheet" href="https://cdn.rcku.cn/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://cdn.rcku.cn/ajax/libs/layui/2.7.6/layui.min.js" charset="utf-8"></script>
</head>

<body style="margin: 5px;">
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layuimini-container">
                <div class="layuimini-main">
                    <div class="alert alert-danger">QQ群图片上传</div>
                    <div class="layui-row layui-col-space15">
                        <div class="panel-body">
                            <p class="text-info">1、仅支持1M以内图片上传，大于1M的图片不会返回链接</p>
                            <p class="text-danger">2、这个图片是上传到QQ群的，应该是不会被删除</p>
                            <p class="text-info">3、需要自己抓取并配置QQ群的cookie（qun.qq.com），还有群号</p>
                        </div>

                        <div class="layui-upload">
                            <div class="layui-upload-list">

                                <p id="demoText"></p>
                                <div class="layui-form-item" id="Img" style="display: none;margin: 10px;">
                                    <label class="layui-form-label">链接</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="ImgUrl" id="ImgUrl" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item" id="Imgyl" style="display: none;margin: 10px;">
                                    <label class="layui-form-label">预览</label>
                                    <div class="layui-input-block">
                                        <img class="layui-upload-img" id="HyImg" style="max-width: 500px;">
                                    </div>
                                </div>
                            </div>
                            <div>
                                <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                                    <div class="layui-progress-bar" lay-percent=""></div>
                                </div>
                            </div>
                            <div style="margin: 10px;">
                                <button type="button" class="layui-btn" id="Submit">上传图片</button>
                            </div>
                        </div>
                    </div>
                </div>
                <script>
                    layui.use(['upload', 'element','layer', 'table'], function() {
                        var $ = layui.jquery,
                            upload = layui.upload,
                            element = layui.element,
                            miniTab = layui.miniTab,
                            layer = layui.layer;
                        table = layui.table;

                        var uploadInst = upload.render({
                            elem: '#Submit',
                            accept: 'file',
                            before: function(obj) {
                                $("#Imgyl").hide();
                                $("#ImgUrl").val('');
                                obj.preview(function(index, file, result) {
                                    //$('#HyImg').attr('src', result);
                                });
                                element.progress('demo', '0%');
                                layer.msg('上传中', {
                                    icon: 16,
                                    time: 0
                                });
                            },
                            done: function(res) {
                                if (res.errno > 0) {
                                    return layer.msg(res.message); // 上传失败
                                }
                                console.log(res.data);
                                $("#ImgUrl").val(res.data);
                                $('#HyImg').attr('src', res.data);
                                $("#Img").show();
                                $("#Imgyl").show();
                                $('#demoText').html('');
                                table.reload('idmallsales');
                            },
                            error: function() {
                                var demoText = $('#demoText');
                                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                                demoText.find('.demo-reload').on('click', function() {
                                    uploadInst.upload();
                                });
                            },
                            progress: function(n, elem, e) {
                                element.progress('demo', n + '%');
                                if (n == 100) {
                                    layer.msg('上传完毕，加载中...', {
                                        icon: 1
                                    });
                                }
                            }
                        });
                    });
                </script>
</body>

</html>